package com.test.study.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * JDBC的工具类
 * @author huyong
 *
 */
public class JdbcUtils {
	private static final Logger LOGGER = Logger.getLogger(JdbcUtils.class.getName());
	
	private static String driver=null;
	private static String username=null;
	private static String password=null;
	private static String url=null;
	static {
		ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
		driver=bundle.getString("driver");
		username=bundle.getString("username");
		password=bundle.getString("password");
		url=bundle.getString("url");
	}


	public static void loadDriver(String driver) {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static void loadDriver() {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection(String url, String username, String password) {
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return connection;
	}

	public static Connection getConnection(String url, String username, String password, String driver) {
		Connection connection = null;
		try {
			loadDriver(driver);
			connection = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
	
	public static Connection getConnection() {
		Connection connection = null;
		try {
			loadDriver(driver);
			connection = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
	public static Connection getConnection(String driver) {
		Connection connection = null;
		try {
			loadDriver(driver);
			connection = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}

	public static void close(Connection connection, Statement statement, ResultSet resultSet) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (Exception e) {
				LOGGER.log(Level.WARNING, e.getMessage());
			}
			resultSet = null;
		}
		if (statement != null) {
			try {
				statement.close();
			} catch (Exception e) {
				LOGGER.log(Level.WARNING, e.getMessage());
			}
			statement = null;
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (Exception e) {
				LOGGER.log(Level.WARNING, e.getMessage());
			}
			connection = null;
		}
	}
	

	public static void close( Statement statement, ResultSet resultSet) {
		if (resultSet != null) {
			try {
				resultSet.close();
			} catch (Exception e) {
				LOGGER.log(Level.WARNING, e.getMessage());
			}
			resultSet = null;
		}
		if (statement != null) {
			try {
				statement.close();
			} catch (Exception e) {
				LOGGER.log(Level.WARNING, e.getMessage());
			}
			statement = null;
		}
		
	}

	public static void close(Connection connection, Statement statement) {

		if (statement != null) {
			try {
				statement.close();
			} catch (Exception e) {
				LOGGER.log(Level.WARNING, e.getMessage());
			}
			statement = null;
		}
		if (connection != null) {
			try {
				connection.close();
			} catch (Exception e) {
				LOGGER.log(Level.WARNING, e.getMessage());
			}
			connection = null;
		}
	}

	public  static void close(Connection connection) {
		if (connection != null) {
			try {
				connection.close();
			} catch (Exception e) {
				LOGGER.log(Level.WARNING, e.getMessage());
			}
			connection = null;
		}
	}

}
